home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter3 / isohex3_7 / isohex3_7.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-18  |  5.9 KB  |  228 lines

  1. /*****************************************************************************
  2. IsoHex3_7.cpp
  3. Ernest S. Pazera
  4. 18MAY2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Requires GDICanvas.h and GDICanvas.cpp
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15. #include "GDICanvas.h"
  16.  
  17. //////////////////////////////////////////////////////////////////////////////
  18. //DEFINES
  19. //////////////////////////////////////////////////////////////////////////////
  20. //name for our window class
  21. #define WINDOWCLASS "ISOHEX3"
  22. //title of the application
  23. #define WINDOWTITLE "IsoHex 3-7"
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //PROTOTYPES
  27. //////////////////////////////////////////////////////////////////////////////
  28. bool Prog_Init();//game data initalizer
  29. void Prog_Loop();//main game loop
  30. void Prog_Done();//game clean up
  31.  
  32. //////////////////////////////////////////////////////////////////////////////
  33. //GLOBALS
  34. //////////////////////////////////////////////////////////////////////////////
  35. HINSTANCE hInstMain=NULL;//main application handle
  36. HWND hWndMain=NULL;//handle to our main window
  37. //gdi canvases
  38. CGDICanvas gdicTile;
  39. CGDICanvas gdicMask;
  40.  
  41. //////////////////////////////////////////////////////////////////////////////
  42. //WINDOWPROC
  43. //////////////////////////////////////////////////////////////////////////////
  44. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  45. {
  46.     //which message did we get?
  47.     switch(uMsg)
  48.     {
  49.     case WM_LBUTTONDOWN:
  50.         {
  51.             //borrow dc from main window
  52.             HDC hdc=GetDC(hWndMain);
  53.  
  54.             //blit from the memory dc to the window's dc
  55.             BitBlt(hdc,LOWORD(lParam)-gdicMask.GetWidth()/2,HIWORD(lParam)-gdicMask.GetHeight()/2,gdicMask.GetWidth(),gdicMask.GetHeight(),gdicMask,0,0,SRCAND);
  56.             BitBlt(hdc,LOWORD(lParam)-gdicTile.GetWidth()/2,HIWORD(lParam)-gdicTile.GetHeight()/2,gdicMask.GetWidth(),gdicMask.GetHeight(),gdicTile,0,0,SRCPAINT);
  57.  
  58.             //return the borrowed dc to the system
  59.             ReleaseDC(hWndMain,hdc);
  60.  
  61.             //handled, so return 0
  62.             return(0);
  63.         }break;
  64.     case WM_DESTROY://the window is being destroyed
  65.         {
  66.  
  67.             //tell the application we are quitting
  68.             PostQuitMessage(0);
  69.  
  70.             //handled message, so return 0
  71.             return(0);
  72.  
  73.         }break;
  74.     case WM_PAINT://the window needs repainting
  75.         {
  76.             //a variable needed for painting information
  77.             PAINTSTRUCT ps;
  78.             
  79.             //start painting
  80.             HDC hdc=BeginPaint(hwnd,&ps);
  81.  
  82.             /////////////////////////////
  83.             //painting code would go here
  84.             /////////////////////////////
  85.  
  86.             //end painting
  87.             EndPaint(hwnd,&ps);
  88.                         
  89.             //handled message, so return 0
  90.             return(0);
  91.         }break;
  92.     }
  93.  
  94.     //pass along any other message to default message handler
  95.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  96. }
  97.  
  98.  
  99. //////////////////////////////////////////////////////////////////////////////
  100. //WINMAIN
  101. //////////////////////////////////////////////////////////////////////////////
  102. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  103. {
  104.     //assign instance to global variable
  105.     hInstMain=hInstance;
  106.  
  107.     //create window class
  108.     WNDCLASSEX wcx;
  109.  
  110.     //set the size of the structure
  111.     wcx.cbSize=sizeof(WNDCLASSEX);
  112.  
  113.     //class style
  114.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  115.  
  116.     //window procedure
  117.     wcx.lpfnWndProc=TheWindowProc;
  118.  
  119.     //class extra
  120.     wcx.cbClsExtra=0;
  121.  
  122.     //window extra
  123.     wcx.cbWndExtra=0;
  124.  
  125.     //application handle
  126.     wcx.hInstance=hInstMain;
  127.  
  128.     //icon
  129.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  130.  
  131.     //cursor
  132.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  133.  
  134.     //background color
  135.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  136.  
  137.     //menu
  138.     wcx.lpszMenuName=NULL;
  139.  
  140.     //class name
  141.     wcx.lpszClassName=WINDOWCLASS;
  142.  
  143.     //small icon
  144.     wcx.hIconSm=NULL;
  145.  
  146.     //register the window class, return 0 if not successful
  147.     if(!RegisterClassEx(&wcx)) return(0);
  148.  
  149.     //create main window
  150.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  151.  
  152.     //error check
  153.     if(!hWndMain) return(0);
  154.  
  155.     //if program initialization failed, then return with 0
  156.     if(!Prog_Init()) return(0);
  157.  
  158.     //message structure
  159.     MSG msg;
  160.  
  161.     //message pump
  162.     for(;;)    
  163.     {
  164.         //look for a message
  165.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  166.         {
  167.             //there is a message
  168.  
  169.             //check that we arent quitting
  170.             if(msg.message==WM_QUIT) break;
  171.             
  172.             //translate message
  173.             TranslateMessage(&msg);
  174.  
  175.             //dispatch message
  176.             DispatchMessage(&msg);
  177.         }
  178.  
  179.         //run main game loop
  180.         Prog_Loop();
  181.     }
  182.     
  183.     //clean up program data
  184.     Prog_Done();
  185.  
  186.     //return the wparam from the WM_QUIT message
  187.     return(msg.wParam);
  188. }
  189.  
  190. //////////////////////////////////////////////////////////////////////////////
  191. //INITIALIZATION
  192. //////////////////////////////////////////////////////////////////////////////
  193. bool Prog_Init()
  194. {
  195.     //borrow dc from main window
  196.     HDC hdc=GetDC(hWndMain);
  197.  
  198.     //load the images
  199.     gdicTile.Load(hdc,"IsoHex3_7-1.bmp");
  200.     gdicMask.Load(hdc,"IsoHex3_7-2.bmp");
  201.  
  202.     //return dc to system
  203.     ReleaseDC(hWndMain,hdc);
  204.  
  205.     return(true);//return success
  206. }
  207.  
  208. //////////////////////////////////////////////////////////////////////////////
  209. //CLEANUP
  210. //////////////////////////////////////////////////////////////////////////////
  211. void Prog_Done()
  212. {
  213.     //destroy the images
  214.     gdicTile.Destroy();
  215.     gdicMask.Destroy();
  216. }
  217.  
  218. //////////////////////////////////////////////////////////////////////////////
  219. //MAIN GAME LOOP
  220. //////////////////////////////////////////////////////////////////////////////
  221. void Prog_Loop()
  222. {
  223.     ///////////////////////////
  224.     //main game logic goes here
  225.     ///////////////////////////
  226. }
  227.  
  228.